package ar.com.temporis.system.domain.usertype;

import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import ar.com.temporis.system.domain.User;
import ar.com.temporis.util.xstream.XStream;


/**
 * 
 * @author matias
 *
 */
public class FooUserType implements UserType {

	private static final int[] SQL_TYPES = { Types.VARCHAR };

	public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
		
		return null;
	}

	public Serializable disassemble(Object arg0) throws HibernateException {
		
		return null;
	}

    public boolean equals(Object x, Object y) {
        return (x == y) || (x != null && x.equals(y));
    }

	public int hashCode(Object x) throws HibernateException {
		if (x == null) {
			return 0;
		}
		return x.hashCode();
	}
	
	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        Reader reader = rs.getCharacterStream(names[0]);
        XStream xStream = getXStream();
        return xStream.fromXML(reader);
	}

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        XStream xStream = getXStream();
        String xml = xStream.toXML(value);
        StringReader r = new StringReader(xml);
        st.setCharacterStream(index, r, xml.length());
    }
	
	private XStream getXStream() {
		XStream xstream = new XStream();
		
		xstream.alias(User.class);
		
		return xstream;
	}

	public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public Class returnedClass() {
		return Object.class;
	}

	public int[] sqlTypes() {
		return SQL_TYPES;
	}
	
    public Object deepCopy(Object value) {
        return value;
    }

    public boolean isMutable() {
        return false;
    }


}
